package com.echat.elocation.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;

import com.echat.elocation.event.NetworkEvent;

import org.greenrobot.eventbus.EventBus;

/**
 *
 * @author lihao
 *
 * @date 2018/7/11 9:57
 */
public class NetworkReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean enable = isNetworkConnected(context);
        EventBus.getDefault().post(new NetworkEvent(enable));
    }

    public boolean isNetworkConnected(Context context) {
        boolean enable = false;
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager != null){
            NetworkInfo info = manager.getActiveNetworkInfo();
            enable = (info != null) && info.isAvailable();
        }
        return enable;
    }

}
